<template>
  <div class="login">
    <div class="login-form">
      <el-form
        :model="LoginForm"
        :rules="rulesLoginForm"
        ref="refLoginForm"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="uesrName">
          <el-input v-model="LoginForm.uesrName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="LoginForm.password" type="password"></el-input>
        </el-form-item>
        <!-- 登录 -->
        <el-form-item class="btn-from">
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import '@/mock/hxfmock.js'
export default {
  name: 'login',
  components: {},
  props: {},
  data() {
    return {
      LoginForm: {
        uesrName: 'admin',
        password: '123456'
      },
      rulesLoginForm: {
        uesrName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 表单重置
    resetForm() {
      this.$refs.refLoginForm.resetFields()
    },
    // 登录
    submitForm() {
      this.$refs.refLoginForm.validate(async valid => {
        if (!valid) {
          return false
        }
        // 发起请求
        const { data } = await this.axios.post('/login', {
          data: this.LoginForm
        })
        // console.log(data)
        if (data.status !== 200) {
          return this.$message.error('用户名或密码输入有误！')
        } else {
          this.$router.push('/home')
          window.sessionStorage.setItem('token', data.data.token)
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/login.jpg') 0 -900px no-repeat;
  background-size: cover;

  .login-form {
    // margin-top: 50px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 1px solid pink;
    background: rgba(255, 255, 255, 0.5);
    width: 500px;
    height: 300px;
    border-radius: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
    /deep/ .el-form-item__label:before {
      content: '';
    }
    .el-form {
      width: 100%;
      .el-form-item {
        margin-right: 50px;
        justify-content: center;
        align-items: center;
      }
    }
    .btn-from {
      margin-left: 200px;
      // margin-top: 100px;
    }
  }
}
</style>
